<?php
// 广告主管理
namespace Admin\Controller;
use Think\Controller;
class ChannelController extends Controller{
	public  function  ChanneldataIssued(){
		$this->isAjaxLogin();
		$datass['input_status']="1";
		$id=I('post.id');
		$model=M('channel_data');
		$data['settlement_status']="1";
		$res=$model->where('id='.$id)->where($datass)->save($data);

		if ($res) {
			$result['code']=200;
			$result['msg']='成功';
		}else{
			$result['code']=501;
			$result['msg']='失败';
		}

		$this->ajaxReturn($result);
	}

	public function  ChannelDataInput(){
		//$this->isAjaxLogin();

		$id=I('post.id/d');
		$modeldd=M('channel_data');
		$ad_data=$modeldd->find($id);
		$channel_id=(int)$ad_data['channel_id'];

		$packet_id=(int)$ad_data['packet_id'];
		$data=I('post.data');
		$cooperation_type=$ad_data['cooperation_type'];
		$model=M('channel_data');
		if($cooperation_type=='cps'){

			$where['start_flow']=array('elt',$data);
			$where['end_flow']=array('gt',$data);
			$divide=M('cps_rule')->where($where)->where([
					'channel_id'=>$channel_id,
					'packet_id'=>$packet_id
				])->getField('divide');

			$datas['divide']=$divide;
			$datas['input_status']="1";
			$datas['real_consumption_amount']=$data;
			$datas['false_consumption_amount']=round((1-$divide/100)*$data,2);;
			$res=$model->where('id='.$id)->save($datas);
		}

		if($cooperation_type=='cpa'){
			$deduction=(int)$ad_data['deduction'];

			$jisuan_re=1-$jisuan_re;
			$datas['false_num']=(int)((100-$deduction)*$data/100);
			$datas['real_num']=(int)$data;
			$unit_price=$ad_data['unit_price'];
			$datas['input_status']="1";

			$datas['real_consumption_amount']=(float)($unit_price*$data);
			$datas['false_consumption_amount']=(float)($datas['false_num']*$unit_price);
			// var_dump($datas['false_num']);die;
			$res=$model->where('id='.$id)->save($datas);


		}
		if ($res) {
			$result['code']=200;
			$result['msg']='录入成功';
		}else{
			$result['code']=501;
			$result['msg']='录入失败';
		}

		$this->ajaxReturn($result);
	}
	public function AddChannel(){
		$this->isAjaxLogin();
		$id=I('post.id');
		if($id){
			$channel_name=I('post.channel_name');
			$contact_name=I('post.contact_name');
			$contact_phone=I('post.contact_phone');
			$user_name=I('post.user_name');
			$password=I('post.password');
			$password=$password;
			$qq=I('post.qq');
			$company_name=I('post.company_name');
			$email=I('post.email');
			$opening_bank=I('post.opening_bank');
			$account=I('post.account');
			$account_name=I('post.account_name');
			$medium_id=I('post.medium_id');

			$data['channel_name'] = $channel_name;
			$data['contact_name'] = $contact_name;
			$data['contact_phone'] = $contact_phone;
			$data['user_name'] = $user_name;
			$data['password'] = $password;
			$data['qq'] = $qq;
			$data['company_name'] = $company_name;
			$data['email'] = $email;
			$data['opening_bank'] = $opening_bank;
			$data['account'] = $account;
			$data['account_name'] = $account_name;
			$data['medium_id'] = $medium_id;
			
			
			$model=M('channel');
			$result=$model->where('id='.$id)->save($data);
			if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '修改成功！';

			}else{

				$ajaxReturn['code'] = 501;
				$ajaxReturn['msg'] = "修改失败！";

			}

			$this->ajaxReturn($ajaxReturn);
		}else{
			$channel_name=I('post.channel_name');
			$contact_name=I('post.contact_name');
			$contact_phone=I('post.contact_phone');
			$user_name=I('post.user_name');
			$password=I('post.password');
			$password=$password;
			$qq=I('post.qq');
			$company_name=I('post.company_name');
			$email=I('post.email');
			$opening_bank=I('post.opening_bank');
			$account=I('post.account');
			$account_name=I('post.account_name');
			$medium_id=I('post.medium_id');

			$data['channel_name'] = $channel_name;
			$data['contact_name'] = $contact_name;
			$data['contact_phone'] = $contact_phone;
			$data['user_name'] = $user_name;
			$data['password'] = $password;
			$data['qq'] = $qq;
			$data['company_name'] = $company_name;
			$data['email'] = $email;
			$data['opening_bank'] = $opening_bank;
			$data['account'] = $account;
			$data['account_name'] = $account_name;
			$data['medium_id'] = $medium_id;
			
			$data['is_delete']="1";
			$data['add_time']=time();
			$model=M('channel');
			$result = $model->add($data);
			
			if($result){

				$ajaxReturn['code'] = 200;
				$ajaxReturn['msg'] = '添加成功！';

			}else{

				$ajaxReturn['code'] = 501;
				$ajaxReturn['msg'] = "添加失败！";

			}

			$this->ajaxReturn($ajaxReturn);
		}
		
	}


	public function  ShowMedia(){
		$this->isAjaxLogin();
		$model=M('manage');
		$result=$model->field('user_id as medium_id,user_name as medium_name')->where('role_id=2')->select();

        if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 501;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
	}

	

		public function  ChannelManagementDisplayById(){
			$this->isAjaxLogin();
		$model=M('channel');
		$page=I('post.page/d');
		$medium_id=I('post.medium_id/d');
		$page_size=I('post.page_size/d');

		$count=$model->where('medium_id='.$medium_id)->count();
		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;
		

		$result=$model->where('medium_id='.$medium_id)->field('id,channel_name,contact_name,contact_phone')->limit($limitStart,$page_size)->select();
		
		

		 if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 501;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
		}

		


		public function MediaDidplayBeforeUpdate(){
			$this->isAjaxLogin();
			$model=M('channel');
			$id=I('post.id');
			$data=$model->where('id='.$id)->find();
            
            if($data !== false){

	            $ajaxReturn['code'] = 200;
	            $ajaxReturn['msg'] = "SUCCESS";
	            $ajaxReturn['data'] = $data;
	        }else{

	            $ajaxReturn['code'] = 501;
	            $ajaxReturn['msg'] = "数据查询失败！";
	        }

	        $this->ajaxReturn($ajaxReturn);
		
		
		}

	

	public function UpdateChannel(){
		$this->isAjaxLogin();
		$id=I('post.id');

		$channel_name=I('post.channel_name');
		$contact_name=I('post.contact_name');
		$contact_phone=I('post.contact_phone');
		$user_name=I('post.user_name');
		$password=I('post.password');
		$password=$password;
		$qq=I('post.qq');
		$company_name=I('post.company_name');
		$email=I('post.email');
		$opening_bank=I('post.opening_bank');
		$account=I('post.account');
		$account_name=I('post.account_name');
		$medium_id=I('post.medium_id');

		$data['channel_name'] = $channel_name;
		$data['contact_name'] = $contact_name;
		$data['contact_phone'] = $contact_phone;
		$data['user_name'] = $user_name;
		$data['password'] = $password;
		$data['qq'] = $qq;
		$data['company_name'] = $company_name;
		$data['email'] = $email;
		$data['opening_bank'] = $opening_bank;
		$data['account'] = $account;
		$data['account_name'] = $account_name;
		$data['medium_id'] = $medium_id;
		
		
		$model=M('channel');
		$result=$model->where('id='.$id)->save($data);
		if($result){

			$ajaxReturn['code'] = 200;
			$ajaxReturn['msg'] = '修改成功！';

		}else{

			$ajaxReturn['code'] = 501;
			$ajaxReturn['msg'] = "修改失败！";

		}

		$this->ajaxReturn($ajaxReturn);
	}

	public function ChannelManagementDisplay(){
		//$this->isAjaxLogin();
		
		$channel_name=I('post.channel_name');
		$contact_name=I('post.contact_name');
		$medium_id=I('post.medium_id/d');


		$where=array();

		if(!empty($channel_name)) {
            $where['channel_name'] = array('like', '%' . $channel_name . '%');
        }

        if(!empty($contact_name)) {
            $where['contact_name'] = array('like', '%' . $contact_name . '%');
        }


        if(!empty($medium_id) && $medium_id!=-1)
         {
            $where['medium_id'] = $medium_id;
        }

		$model=M('channel');
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');

		$count=$model->where($where)->where('is_delete="1"')->count();
		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;

		$result=$model->where($where)->where('is_delete="1"')->field('id,channel_name,contact_name,contact_phone')->limit($limitStart,$page_size)->select();
		
		$ajaxReturn['data']['list'] = $result;
        $ajaxReturn['data']['total_page'] = $count;
		 // if($result){

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "查询成功";
            


        // }else{

        //     $ajaxReturn['code'] = 501;
        //     $ajaxReturn['msg'] = "查询成功";
        // }

        $this->ajaxReturn($ajaxReturn);
	}

	public function DeleteChannel(){
		//$this->isAjaxLogin();
		$id=I('post.id/d');
		$pamodel=M('channel_packet');
		$paresult=$pamodel->where('channel_id='.$id)->select();
 		foreach ($paresult as $key => $value) {
 			$arrsum[]=(int)$value['bound_state'];
 		}
 		$arr=array_sum($arrsum);
 		if($arr==0){
 			$model=M('channel');
			$data['is_delete']="0";
			$result=$model->where('id='.$id)->save($data);

			 if($result){

	            $ajaxReturn['code'] = 200;
	            $ajaxReturn['msg'] = "SUCCESS";
	           


	        }
 		}else{

	            $ajaxReturn['code'] = 501;
	            $ajaxReturn['msg'] = "绑定后不能删除";
	        }
		

        $this->ajaxReturn($ajaxReturn);
	}

	


	

	//渠道数据录入列表
	public function  channelDataList(){
		//$this->isAjaxLogin();

		$channel_id=I('post.channel_id');
		$ad_id=I('post.ad_id');
		$start_time=I('post.start_time');
		$end_time=I('post.end_time');
		$settlement_status=I('post.settlement_status');
		$settlement_status=I('post.settlement_status');

		$where=array();

		if(!empty($channel_id) && $channel_id!=-1 ) {
            $where['channel_id'] = $channel_id;
        }

        if(!empty($ad_id) && $ad_id!=-1) {
            $where['ad_id'] = $ad_id;
        }
        if (in_array($settlement_status,['0','1'])) {
			$where['settlement_status']=$settlement_status;	 
		}

		if (in_array($input_status,['0','1'])) {
			$where['input_status']=$input_status;	 
		}

        

        if (!empty($start_time)&&!empty($end_time)) {

        	$where['add_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');
		$model=M('channel_data');
		$count=$model->where($where)->count();

		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;
		$data=$model->where($where)->limit($limitStart,$page_size)->select();
		$data=array_map(function($val){
			if($val['cooperation_type']=='cps'){
				$val['data']=$val['real_consumption_amount'];
				$val['put_price']=$val['divide'];
			    $val['add_time']=date("Y-m-d",$val['add_time']);


			}elseif($val['cooperation_type']=='cpa'){
				$val['data']=$val['real_num'];
				$val['put_price']=$val['unit_price'];
			    $val['add_time']=date("Y-m-d",$val['add_time']);

			}
			
			return $val;
		}, $data);
		
		// if ($data) {
			$result['code']=200;
			$result['msg']='成功';
			$result['data']['total_page']=$count;
			$result['data']['list']=$data;
		// }else{
		// 	$result['code']=501;
		// 	$result['msg']='查询成功';
		// }
		$this->ajaxReturn($result);
	}

	//结算订单列表
	public function ClearingHouseDisplay(){
		$this->isAjaxLogin();

		$channel_id=I('post.channel_id');
		$packet_id=I('post.packet_id');
		$start_time=I('post.start_time');
		$end_time=I('post.end_time');
		$balance_status=I('post.balance_status');

		$where=array();

		if(!empty($channel_id) && $channel_id!=-1) {
            $where['channel_id'] = $channel_id;
        }

        if(!empty($packet_id) && $packet_id!=-1) {
            $where['packet_id'] = $packet_id;
        }
        if(!empty($balance_status) && $balance_status!=-1) {
            $where['balance_status'] = $balance_status;
        }


        

        if (!empty($start_time)&&!empty($end_time)) {

        	$where['add_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }

		$page=I('post.page/d');
		$page_size=I('post.page_size/d');
		$model=M('channel_data');
		$count=$model->where('settlement_status="1"')->where($where)->count();

		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;
		$data=$model->where('settlement_status="1"')->where($where)->limit($limitStart,$page_size)->select();

		$data=array_map(function($val){
			if($val['cooperation_type']=='cps'){
				$val['data']=$val['real_consumption_amount'];
			    $val['add_time']=date("Y-m-d",$val['add_time']);

			}elseif($val['cooperation_type']=='cpa'){
				$val['data']=$val['real_num'];
			    $val['add_time']=date("Y-m-d",$val['add_time']);

			}
			
			return $val;
		}, $data);
		
		// if ($data) {
			$result['code']=200;
			$result['msg']='成功';
			$result['data']['total_page']=$count;
			$result['data']['list']=$data;
		// }else{
		// 	$result['code']=501;
		// 	$result['msg']='查询成功';
		// }
		$this->ajaxReturn($result);

	}

	//发起结算订单
	public function  ProduceSettlementOrder(){
		//$this->isAjaxLogin();
		$channel_data_id_list=I('post.id');

		 $mount_id=explode(',', $channel_data_id_list);

		foreach ($mount_id as $k => $v) {
			$modelid=M('channel_data');
			$aass[]=$modelid->getField('balance_status');
		 	$result_id[]=$modelid->where('id='.$v)->field('false_consumption_amount')->find();
		 	$channel_id=$modelid->where('id='.$v)->field('channel_id')->find();
		 	$channel_id=(int)$channel_id['channel_id'];
		 	$channel_name=$modelid->where('id='.$v)->field('channel_name')->find();
		 	$channel_name=$channel_name['channel_name'];
		 }
		 	$aass=array_sum($aass);


		 foreach ($result_id as $key => $value) {
		 	$resultid[]=$value['false_consumption_amount'];
		 }

		 //var_dump($resultid);die;
		 $resultsum=array_sum($resultid);

		$order_time="JS".date('YmdHis',time());
		$random1=md5(uniqid());
		$random=substr($random1, 20);
		$order_num=$order_time.$random;
		$generation_time=time();
		$user_info=session('user_info'); 
		$data['balance_status']="0";
		$data['user_id']=$user_info['user_id'];//
		$data['user_name']=$user_info['user_name'];//
		$data['order_num']=$order_num;//
		$data['channel_name']=$channel_name;//
		$data['channel_id']=$channel_id;//
		$data['channel_data_id_list']=$channel_data_id_list;//
		$data['generation_time']=$generation_time;//
		$data['invoice_status']="0";//
		$data['pay_status']="0";//
		$data['settlement_amount']=$resultsum;//
		$data['order_status']="1";//
		$datamodel=M('channel_data');
		foreach ($mount_id as $k => $v) {
			$datas['balance_status']="1";
			$datamodel->where('id='.$v)->save($datas);
		}
		$modeldata=M('settlement_order');
		if($aass<1){
			$resultdata=$modeldata->add($data);
			$result['code']=200;
			$result['msg']='录入成功';
		}
		

		
		else{
			$result['code']=501;
			$result['msg']='包含结算的渠道包';
		}
		$this->ajaxReturn($result);
	}

}
